﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WebUI.Models;

namespace WebUI.HtmlHelpers
{
    public static class PagingHelpers
    {
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl)
        {
            var result = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                var tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                    tag.AddCssClass("selected");
                result.Append(tag.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString PageAjaxLinks(this HtmlHelper html, PagingInfo pagingInfo, AjaxOptions ajaxOptions, Func<int, string> pageUrl)
        {
            var result = new StringBuilder();

            AppendTag(result, 1, "Trang đầu", pagingInfo.CurrentPage, pageUrl(1), ajaxOptions);
            AppendTag(result, pagingInfo.CurrentPage - 1, "Trang trước", pagingInfo.CurrentPage, pageUrl(pagingInfo.CurrentPage - 1), ajaxOptions);

            if (pagingInfo.CurrentPage > 4)
            {
                var tagDot = new TagBuilder("a") { InnerHtml = "..." };
                result.Append(tagDot);
            }

            int pageIndex = pagingInfo.CurrentPage > 4 ? pagingInfo.CurrentPage - 4 : 1;

            for (int i = 1; i <= 9 && pageIndex <= pagingInfo.TotalPages; i++)
            {

                AppendTag(result, pageIndex, pageIndex.ToString(), pagingInfo.CurrentPage, pageUrl(pageIndex), ajaxOptions);

                pageIndex++;
            }

            if ((pagingInfo.TotalPages - pagingInfo.CurrentPage) > 4)
            {
                var tag = new TagBuilder("a") { InnerHtml = "..." };
                result.Append(tag);
            }

            AppendTag(result, pagingInfo.CurrentPage + 1, "Trang sau", pagingInfo.CurrentPage, pageUrl(pagingInfo.CurrentPage + 1), ajaxOptions, true, pagingInfo.TotalPages);
            AppendTag(result, pagingInfo.TotalPages, "Trang cuối", pagingInfo.CurrentPage, pageUrl(pagingInfo.TotalPages), ajaxOptions);

            return MvcHtmlString.Create(result.ToString());
        }

        private static void AppendTag(StringBuilder result, int pageIndex, string text, int currentPage, string pageUrl, AjaxOptions ajaxOptions, bool isNext = false, int total = 0)
        {
            if (pageIndex == 0)
                return;
            var tag = new TagBuilder("a") { InnerHtml = text };
            if (isNext && currentPage == total)
            {
                result.Append(tag);
            }
            else
            {
                //tag.MergeAttribute("href", pageUrl);
                if (pageIndex == currentPage && text == pageIndex.ToString())
                    tag.AddCssClass("current-page");

                //ajaxOptions.Url = pageUrl;
                //tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
                result.Append(tag);
            }

        }
    }
}